﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
using Excel;
using System.Data;
using System.Reflection;

namespace ExportExcel
{

    public class ExcelReader: Singleton<ExcelReader>
    {

        public void Load(ExportConfig config)
        {
            m_config = config;
            m_fileDic.Clear();
            if (Directory.Exists(m_config.ExcelPath))
            {
                string[] files =Directory.GetFiles(m_config.ExcelPath, "*", SearchOption.AllDirectories);
                if(files.Length > 0)
                {
                    CodeCreater.CreateClientBaseCode(m_config.ClientLanguage,m_config.ClientCodeOutPutPath + "/code");
                    CodeCreater.CreateServerBaseCode(m_config.ServerLanguage, m_config.ServerCodeOutPutPath + "/code");

                    for (int i = 0; i < files.Length; i++)
                    {
                        string ext = Path.GetExtension(files[i]).ToLower();
                        string name = Path.GetFileName(files[i]).ToLower();
                        if (ext == ".xlsx")
                        {
                            ExcelFile file = new ExcelFile(files[i]);
                            m_fileDic.Add(name, file);
                            CodeCreater.CreateClientCode(file,m_config.ClientLanguage, m_config.ClientCodeOutPutPath + "/code");
                            file.SaveClientData(m_config.ClientCodeOutPutPath + "/data");
                            CodeCreater.CreateServerCode(file, m_config.ServerLanguage, m_config.ServerCodeOutPutPath + "/code");
                            file.SaveServerData(m_config.ServerCodeOutPutPath + "/data");
                        }
                    }
                }
            }

        }

        private ExportConfig m_config = null;
        private Dictionary<string, ExcelFile> m_fileDic = new Dictionary<string, ExcelFile>();


    }

}
